<?php
namespace App\Http\Controllers;


//礼包活动
use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Services\ConfigServices;
use App\Http\Services\GiftBagIntimacyServices;
use App\Http\Services\GiftBagRechargeLogServices;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class GiftBagController extends Controller
{

    protected $configServices;
    protected $giftBagRechargeLogServices;
    protected $giftBagIntimacyServices;

    public function __construct()
    {
        //监听充值
        $this->configServices = new ConfigServices();
        $this->giftBagRechargeLogServices = new GiftBagRechargeLogServices();
        $this->giftBagIntimacyServices = new GiftBagIntimacyServices();
    }

    public function assignAction($body)
    {
        $data = json_decode($body, true);
        if (isset($data['diamond_amount'])) {
            $this->listenUserRechareGiftBag($data);
        } else if (isset($data['gift_id'])) {
            $this->listenIntimacyValue($data);
        } else {
            return true;
        }
    }


    //监听用户充值
    public function listenUserRechareGiftBag($body)
    {
        $user_id = $body['user_id'] ?? 0;
        $diamond_num = $body['diamond_amount'] ?? 0;
        $timestamp = $body['pay_timestamp'] ?? 0;
        //支付类型 --- 0.支付宝 1.微信 2.苹果内购 3.android公号 4.IOS公众号 5.后台代充 6.支付宝wap端充值 7.微信wap端充值
        $pay_type = $body['pay_type'];
        $rank_arr = ['600', '3000', '9800'];
        if($pay_type == 2){
            $rank_arr = ['420','2100','6880'];
        }
        $diamond_num_type = array_flip($rank_arr);
        if (empty($user_id) || empty($diamond_num)) {
            write_log('giftBagConsumer', 'bodyError', json_encode($body));//数据错误
            return false;
        }
        $config = $this->configServices->getConfig(3);
        //判定用户充值等级
        if (!isset($diamond_num_type[$diamond_num])) {
            write_log('giftBagConsumer', 'rechargeStandard', json_encode($body));//不达标
            return false; //充值不达标
        }
        $rank = $diamond_num_type[$diamond_num] + 1; //1 600 2 3000 3 9800
        $config_date = $config['gift_bag_date'];
        $checkConfigDate = $this->configServices->checkConfigDate($config_date, $timestamp);
        if (!$checkConfigDate) {
            write_log('giftBagConsumer', 'timeError', json_encode($body));//
            return false;
        }
        //验证用户是否在当前时间内 &是否在活动时间内 & 是否当天领取过该礼包
        $checkRecharege = $this->giftBagRechargeLogServices->sendLog($user_id, $rank);
        if (!$checkRecharege) {
            write_log('giftBagConsumer', 'repetitionRecharge', $user_id . '-' . $rank);//重复充值等级
            return false;
        }
        $pearl_rand = rand(12, 120); //随机赠送金豆数量
        switch ($rank) {
            case 2;
                $pearl_rand = rand(60, 600);
                break;
            case 3;
                $pearl_rand = rand(196, 1960);
                break;
            default;
        }
        //生成用户可领取记录
        $rechargeLog = $this->giftBagRechargeLogServices->createRechargeLog($user_id, $rank, $diamond_num, $pearl_rand);
        if (!$rechargeLog) {
            return false;
        }
        write_log('giftBagConsumer', 'success', $user_id);
        return true;
    }

    //查看当日用户是否领取礼包
    public function userGiftBagRank(Request $request)
    {
        $user_id = $request->input('user_id');
        $res = $this->giftBagRechargeLogServices->giftBagRechargeLog($user_id);
        OutJson::outJsonResult(Hint::SUCCESS, $res);//
    }

    //用户点击领取礼包
    /*code
     * 200 成功
     * 201 该等级未充值
     * 202已经领取
     * 203/领取失败
     * */
    public function clickGiftBag(Request $request)
    {
        $user_id = $request->input('user_id') ?? 0;
        $rank = $request->input('rank') ?? 1;
        $res['code'] = $this->giftBagRechargeLogServices->giftBagClickGet($user_id, $rank);
        OutJson::outJsonResult(Hint::SUCCESS, $res);
    }


    //监听亲密值
    public function listenIntimacyValue($body)
    {
        $user_id = $body['user_id'];//送用户
        $to_user = $body['to_user_id_array'];//收取用户
        $gift_id = $body['gift_id'];//礼物id
        $gift_type = $body['type'];// 0=CBD礼物 1=CBD礼物-背包 2=小屋礼物 3=小屋礼物-背包
        $timestamp = $body['create'];//赠送时间
        $num = $body['gift_num'] ?? 1;//赠送个数
        $config = $this->configServices->getConfig(3);
        $config_date = $config['gift_bag_intimacy_date'];
        $config_gift_id = $config['gift_bag_gift_ids']['value'];//获取配置礼物id
        if (!in_array($gift_id, explode(',', $config_gift_id))) {
            write_log('intimacyValueConsumer', 'giftIdError', $user_id . '-' . $gift_id);//礼物非活动礼物
            return false;
        }
        $checkConfigDate = $this->configServices->checkConfigDate($config_date, $timestamp);
        if (!$checkConfigDate) {
            write_log('intimacyValueConsumer', 'timeError', json_encode($body));//过期礼物
            return false;
        }
        if (in_array($gift_type, [0, 1])) {
            //钻石礼物进行排名
            $diamond_res = $this->giftBagIntimacyServices->getRankList($user_id, $to_user, $num);
            if (!$diamond_res) {
                write_log('intimacyValueConsumer', 'diamondError', json_encode($body));//钻石礼物排行榜错误
                return false;
            }
        } else if (in_array($gift_type, [2, 3])) {
            //验证cp
            $checkCp = $this->giftBagIntimacyServices->checkCpUser($to_user);
            if (!$checkCp) {
                write_log('intimacyValueConsumer', 'cpError', json_encode($body));//礼物非活动礼物\\
                return false;
            }
            //添加用户亲密值
            $res = $this->giftBagIntimacyServices->createIntimacyLog($checkCp, $num);
            if (!$res) {
                write_log('intimacyValueConsumer', 'intimacyValueError', $user_id . '-' . $gift_id);//礼物非活动礼物
                return false;
            }
        } else {
            write_log('intimacyValueConsumer', 'typeError', $user_id . '-' . $gift_id);//礼物非活动礼物
        }
        write_log('intimacyValueConsumer', 'success', $user_id);//礼物非活动礼物
        return true;
    }


    //补发亲密值
    public function intimacyCheck()
    {
        $time[] = '1563285600';//开始
        $time[] = '1563352616';//截至时间戳
        $res = $this->giftBagIntimacyServices->checkValue($time);
        OutJson::outJsonResult(Hint::SUCCESS, $res);//
    }

    //补发日志表
    public function editLog()
    {
        $time[] = '1563285600';//开始
        $time[] = '1563352616';//截至时间戳
        $res = $this->giftBagIntimacyServices->logCreate($time);
        OutJson::outJsonResult(Hint::SUCCESS, $res);//
    }


    //获取个人亲密值积分
    public function getUserIntimacy(Request $request)
    {
        $user_id = $request->input('user_id') ?? 0;
        $user_intimacy = $this->giftBagIntimacyServices->getIntimacy($user_id) ?? 0;
        OutJson::outJsonResult(Hint::SUCCESS, $user_intimacy);//
    }

    //获取送礼榜收礼榜
    public function getGiftRank(Request $request)
    {
        $user_id = $request->input('user_id') ?? 0;
        $rank = $this->giftBagIntimacyServices->getGiftRank($user_id);
        OutJson::outJsonResult(Hint::SUCCESS, $rank);//
    }


    //发放礼物
    public function awardGiftToRank()
    {
        $res = $this->giftBagIntimacyServices->awardGift();
        OutJson::outJsonResult(Hint::SUCCESS, $res);//
    }

    public function giftRank(){
        $res = $this->giftBagIntimacyServices->getRank();
        OutJson::outJsonResult(Hint::SUCCESS, $res);//

    }
    //补发礼物
    public function anewAwardGift()
    {
        $res = $this->giftBagIntimacyServices->anewAwardGift();
        OutJson::outJsonResult(Hint::SUCCESS, $res);//
    }



}